﻿// We are given integer number n, value v (v=0 or 1) and a position p. Write a sequence of operators that modifies n 
// to hold the value v at the position p from the binary representation of n.
//	Example: n = 5 (00000101), p=3, v=1 -> 13 (00001101)
//	n = 5 (00000101), p=2, v=0 -> 1 (00000001)

using System;

class ModifyValueAtPosition
{
    static void Main()
    {
        Console.Write("Enter integer number: ");
        int number = int.Parse(Console.ReadLine());
        Console.Write("Enter value 0 or 1 : ");
        int value = int.Parse(Console.ReadLine());
        Console.Write("Enter position: ");
        int position = int.Parse(Console.ReadLine());

        int mask = 1;
        int maskPosition;
        int numberAndMask;
        if (value == 0)
        {
            maskPosition = ~(mask << position);
            numberAndMask = number & maskPosition;
        }
        else
        {
            maskPosition = mask << position;
            numberAndMask = number | maskPosition;
        }
        
        Console.WriteLine("After modifying bit {0} to {1} the result is {2}", position, value, numberAndMask);
    }
}
